package com.younion.action.consumer.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IAdvertisingService;
import com.younion.common.constants.SysConstants;
import com.younion.model.AdvertisementListResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_AdvertisingVO;

public class AdvertisementGetConsumer extends AbstractRequestConsumer {
	private IAdvertisingService advertisingService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getAdvertisement(request, response);
	}

	public String getAdvertisement(HttpServletRequest request,
			HttpServletResponse response) {
		String zone = request.getParameter("zone");// 1-轮换 0-首页
		String client = request.getParameter("client");
		Map paramMap = new HashMap();
		paramMap.put("zone", zone);
		ResultObject resultObject = new ResultObject();
		List<AdvertisementListResult> advertisementResultList = new ArrayList<AdvertisementListResult>();
		List<API_AdvertisingVO> advList = null;
		try {
			advList = advertisingService.getApiAdvertisement(paramMap);
			if (advList != null && advList.size() > 0) {
				String img_url_pre = ConfigHelper.fastGetConfig("interface",
						"img_url_pre", "http://localhost:8080/younonmember/");
				for (API_AdvertisingVO api_AdvertisingVO : advList) {
					AdvertisementListResult advertisementListResult = new AdvertisementListResult();
					advertisementListResult.setId(api_AdvertisingVO.getId()
							.toString());
					advertisementListResult.setLinkId(api_AdvertisingVO
							.getLinkId() != null ? api_AdvertisingVO
							.getLinkId().toString() : "");
					advertisementListResult.setLinkType(api_AdvertisingVO
							.getLinkType().toString());
					advertisementListResult.setLinkUrl(api_AdvertisingVO
							.getLinkUrl());
					advertisementListResult.setTop(api_AdvertisingVO.getTop()
							.toString());
					String[] picName_array = api_AdvertisingVO.getPicNames()
							.split(",");
					advertisementListResult.setPic("");
					if (picName_array != null) {
						if (zone != null && "0".equals(zone)) {// 首页
							if (client != null && "android".equals(client)) {// android
								for (String picName : picName_array) {
									if (picName
											.indexOf(ApiConstant.ANDROID_ADV_RESOLUTION_1) >= 0) {
										advertisementListResult
												.setPic(img_url_pre
														+ SysConstants.ADVERTISING_IMG_URL
														+ "/"
														+ api_AdvertisingVO
																.getId() + "/"
														+ picName);
										break;
									}
								}
							} else if (client != null
									&& "iphone".equals(client)) {// iphone
								for (String picName : picName_array) {
									if (picName
											.indexOf(ApiConstant.IPHONE_ADV_RESOLUTION_1) >= 0) {
										advertisementListResult
												.setPic(img_url_pre
														+ SysConstants.ADVERTISING_IMG_URL
														+ "/"
														+ api_AdvertisingVO
																.getId() + "/"
														+ picName);
										break;
									}
								}
							}
						} else if (zone != null && "1".equals(zone)) {// 轮换
							if (client != null && "android".equals(client)) {// android
								for (String picName : picName_array) {
									if (picName
											.indexOf(ApiConstant.ANDROID_ADV_RESOLUTION_2) >= 0) {
										advertisementListResult
												.setPic(img_url_pre
														+ SysConstants.ADVERTISING_IMG_URL
														+ "/"
														+ api_AdvertisingVO
																.getId() + "/"
														+ picName);
										break;
									}
								}
							} else if (client != null
									&& "iphone".equals(client)) {// iphone
								for (String picName : picName_array) {
									if (picName
											.indexOf(ApiConstant.IPHONE_ADV_RESOLUTION_2) >= 0) {
										advertisementListResult
												.setPic(img_url_pre
														+ SysConstants.ADVERTISING_IMG_URL
														+ "/"
														+ api_AdvertisingVO
																.getId() + "/"
														+ picName);
										break;
									}
								}
							}
						}
					}
					advertisementResultList.add(advertisementListResult);
				}
			}
			resultObject.setResult(advertisementResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	public void setAdvertisingService(IAdvertisingService advertisingService) {
		this.advertisingService = advertisingService;
	}
}
